package com.hzya.frame.utils.core.tree.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzya.frame.utils.core.tree.BaseTreeNodeDomain;
import com.hzya.frame.utils.core.tree.ITreeNodeGenerator;
import com.hzya.frame.utils.core.tree.TreeNode;

import java.util.Map;

public class EasyuiTreeNodeGenerator implements ITreeNodeGenerator {
	public Object createTreeNode(BaseTreeNodeDomain baseTreeNodeDomain) {
		TreeNode treeNode = new TreeNode();
		treeNode.setId(Long.valueOf(baseTreeNodeDomain.getId()));
		treeNode.setText(baseTreeNodeDomain.getName());
		treeNode.setParentId(
				Long.valueOf((baseTreeNodeDomain.getParentId() == null) ? "0" : baseTreeNodeDomain.getParentId()));
		treeNode.setState(baseTreeNodeDomain.getState());
		treeNode.setChecked(Boolean.valueOf(false));
		ObjectMapper objectMapper = new ObjectMapper();
		Map attributes = baseTreeNodeDomain.getAttributesExp();
		if (attributes != null) {
			try {
				String resultStr = objectMapper.writeValueAsString(attributes);
				treeNode.setAttributes(resultStr);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

		}

		return treeNode;
	}
}